#!/usr/bin/env python

# (c) 2007-2011 Helsinki University of Technology and University of Helsinki
# Licensed under the MIT license; see LICENSE.txt for more information.

from harava.document import Document
from harava.rdf import *
from rdf import RDFImporter

import sys
import html5lib
from cStringIO import StringIO
import pyRdfa
from pyRdfa.Options import Options, HTML5_RDFA

class RDFaImporter (RDFImporter):
  def __init__(self, session):
    RDFImporter.__init__(self, session)

  def process(self, doc):
    data = StringIO(doc.getData())
    
    parser = html5lib.HTMLParser(tree=html5lib.treebuilders.getTreeBuilder("dom"))
    dom = parser.parse(data)

    options = Options()
    options.host_language = HTML5_RDFA
    rdf = getGraph()

    success = True
    try:
      pyRdfa.parseRDFa(dom, doc.getURL(), rdf, options)
    except:
      success = False

    data.close()
    
    if success:
      self._rdf += rdf
    else:
      print >>sys.stderr, "RDFa parsing for %s failed." % doc
    
    return success
  
  # NOTE: the RDFImporter.finish() method is used to process the parsed RDF
    
